library(data.table) #Load all required packages
library(rvest)
library(lubridate)
library(dbplyr)
library(DataComputing)
library(magrittr)
library(Hmisc)
library(party)
library(rpart)
rm(list = ls()) #Clean up environment
This dataset contains policy data for 50 US states and DC from 2001 to 2017. Data include information related to state legislation and regulations on nutrition, physical activity, and obesity in settings such as early care and education centers, restaurants, schools, work places, and others. To identify individual bills, use the identifier ProvisionID. A bill or citation may appear more than once because it could apply to multiple health or policy topics, settings, or states.
getwd() #Get working directory
[1] "/Users/fun.k/Final-Project"
setwd("~/Downloads") #Set working directory
The working directory was changed to /Users/fun.k/Downloads inside a notebook chunk. The working directory will be reset when the chunk is finished running. Use the knitr root.dir option in the setup chunk to change the working directory for notebook chunks.
untidy <-read.csv("CDC_Nutrition__Physical_Activity__and_Obesity_-_Legislation.csv") #Read file in table format and create a data frame from it
gc(reset=TRUE) #Change from data frame to data table
used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
Ncells 3452027 184.4 5459388 291.6 NA 3452027 184.4
Vcells 11295443 86.2 26229870 200.2 16384 11295443 86.2
tracemem(untidy)
[1] "<0x11da8db30>"
untidy <- as.data.table(untidy)
tracemem[0x11da8db30 -> 0x10bd770e0]: copy as.data.table.data.frame as.data.table
tracemem[0x11da8db30 -> 0x11dafa4c0]: as.list.data.frame as.list vapply copy as.data.table.data.frame as.data.table
gc()
used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
Ncells 3452024 184.4 5459388 291.6 NA 3457090 184.7
Vcells 11297487 86.2 26229870 200.2 16384 11765330 89.8
This dataset can be useful in looking at
untidy #Take a look
#Clean Data
#Change variables to start with lowercase/ make easier to use
Legislation <-
untidy %>%
rename(year = Year, quarter = Quarter, state = LocationAbbr, healthTopic = HealthTopic, policyTopic = PolicyTopic, dataSource = DataSource, setting = Setting, title = Title, status = Status, citation = Citation, statusAltValue = StatusAltValue, dataType = DataType, comments = Comments, enDate = EnactedDate, effDate = EffectiveDate, coordinates = GeoLocation, display = DisplayOrder)
Legislation
We can see how much legislation has been passed in certain years, which years have been the most progressive, which states have more legislation than others, which topic has been most popular, and more. This information can give government officials the ability to see where certain states need to improve, which topics need to be emphasized, and more.
yearlyData <-
Legislation %>%
group_by(year) %>%
summarise(total = sum(year)) %>%
mutate(total = total/1000)
yearlyData %>%
ggplot(aes(x=year,y=total ))+
geom_bar(stat='identity',position='stack', width=.9, fill = "red") +
ylab("Total (1000s)") +
xlab("Year")
Each bar in the chart represents one year. One can see that in 2011, the most legislation/regulation has been passed between 2001 and 2017.
#coordinates <- Legislation[,17]
#long <- sapply(Legislation$coordinates, "[", 1)
#lat <- sapply(Legislation$coordinates, "[", 2)
#long
#predictedStatus <-
#Legislation %>%
#sample_n(200) %>%
#rpart(status ~ year + state, data = .)
#rpart.plot(predictedStatus, roundint = FALSE)
Legislation %>%
sample_n(200) %>%
group_by(year, healthTopic, PolicyTypeID) %>%
summarise(total = sum(year)) %>%
mutate(total = total/1000) %>%
ggplot(aes(x= year, y = total)) +
geom_point(color = "red") +
facet_grid(PolicyTypeID ~ healthTopic) +
ylab("total (1000s)") +
xlab("Year")
As we can see, there has been a lot less regulation than legislation. Nutrititional legislation appears to be the most popular with an exponentially growing total without counting years after about 2012.
predictedStatus <-
Legislation %>%
sample_n(20) %>%
rpart(status ~ year + state, data = .)
Error in eval(lhs, parent, parent) : object 'Legislation' not found